import { Component, OnInit, ViewChild } from '@angular/core';
import { HttpRequest } from "../../../service/httpRequest";
import { HideElementService } from "../../../service/hideElement.service";
import { PageModel } from "../../../model/page-model";
import { flyIn } from '../../../animations/fly-in';
import { GlobalVariable } from '../../../global.config';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { FileLoader } from "../../../service/fileLoader";
declare var $:any;
@Component({
  selector: 'app-device',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css'],
  animations: [
    flyIn
  ]
})
export class ProductComponent implements OnInit {
  elc = {a:'产品新增',e:'产品编辑',d:'产品删除',o:'设备列表',p:'产品数据点'};
  @ViewChild('myModal') public myModal:ModalDirective;
  buttonType:number = 0;
  disabled:string = '';
  model = {name:'',brand:'',description:'',monitor_path:'',img_path:'',qrcode_path:''};
  selectedId:string = '';

  inited:boolean = false;

  tool = GlobalVariable.tooltip;
  data:any;
  keyword:any;
  pageModel = new PageModel();
  page:number = 1;
  constructor(private httpRequest:HttpRequest,private fileLoader:FileLoader,private he:HideElementService) {
    this.elc = this.he.hideElement(this.elc);
  }

  ngOnInit() {
    this.getData();
    this.fileLoader.LoadFileList(['assets/js/uploadify/jquery.uploadify.min.js'], function(){});

  }

  getData(){
    this.httpRequest.sendGet('products/page',{page:this.page}).subscribe(res=>{
      this.data = res.result.aaData;
      this.pageModel.setPage({'iTotalRecords':res.result.iTotalRecords});
    },err=>console.log(err));
  }


  newModal(){
    this.initUploader();
    this.buttonType=1;
    this.model = {name:'',brand:'',description:'',monitor_path:'',img_path:'',qrcode_path:''};
    this.myModal.show();
  }


  confirmNew(){
    this.model.img_path = $("#file_uploaded1").val();
    this.model.qrcode_path = $("#file_uploaded2").val();
    this.httpRequest.sendPost('products',{
      name: this.model.name,
      brand: this.model.brand,
      description: this.model.description,
      monitor_path: this.model.monitor_path,
      img_path: this.model.img_path,
      qrcode_path: this.model.qrcode_path
    }).subscribe(res=>{
      alert("产品创建成功！");
      this.myModal.hide();
      this.disabled='';
      this.getData();
    },err => {alert('产品创建失败!');this.disabled='';console.log(err)});
  }

  editModal(item){
    this.initUploader();
    this.buttonType=2;
    this.selectedId = item.id;
    //this.model = {selectoption:item.product_id,name:item.name,man:item.lxr,phone:item.lxdh};
    this.model = item;
    this.myModal.show();
  }

  confirmEdit(){
    this.httpRequest.sendPut('products/'+this.selectedId,{
      name: this.model.name,
      brand: this.model.brand,
      description: this.model.description,
      monitor_path: this.model.monitor_path,
      img_path: this.model.img_path,
      qrcode_path: this.model.qrcode_path
    }).subscribe(res=>{
      alert("产品修改成功！");
      this.myModal.hide();
      this.disabled='';
      this.getData();
    },err => {alert('产品修改失败!');this.disabled='';console.log(err)});
  }

  deleteModal(item){
    this.buttonType=3;
    this.selectedId = item.id;
    this.model.name = item.name;
    this.myModal.show();
  }

  confirmDelete(){
    this.httpRequest.sendDelete('products/'+this.selectedId).subscribe(res=>{
      alert("产品删除成功！");
      this.myModal.hide();
      this.disabled='';
      this.getData();
    },err => {alert('产品删除失败!');this.disabled='';console.log(err)});
  }


  //初始化图片上传控件
  initUploader(){
    if(this.inited == true) return;
    this.inited = true;
    let that =this;
    $('#file_upload').uploadify({
      'swf'      : 'assets/js/uploadify/uploadify.swf',
      'uploader' : "http://101.37.24.73:8080/fileupload/",
      'buttonText': '上传产品图片',
      'fileObjName' : 'file',
      'fileTypeDesc' : '选择文件',
      'fileTypeExts' : '*.jpg;*.png;',
      'method' : 'post',
      'fileSizeLimit':'10MB',
      'auto' : true,
      'multi' : false,
      'onUploadSuccess' : function(file,data,response) {
        //console.log(data);
        var filedata = JSON.parse(data);
        that.model.img_path = filedata.file_path;
        $("#file_uploaded1").val(filedata.file_path);
        $("#file_uploaded11").text(filedata.file_path+"上传完成");
      }
    });
    $('#file_upload1').uploadify({
      'swf'      : 'assets/js/uploadify/uploadify.swf',
      'uploader' : "http://101.37.24.73:8080/fileupload/",
      'buttonText': '上传二维码图片',
      'fileObjName' : 'file',
      'fileTypeDesc' : '选择文件',
      'fileTypeExts' : '*.jpg;*.png;',
      'method' : 'post',
      'fileSizeLimit':'10MB',
      'auto' : true,
      'multi' : false,
      'onUploadSuccess' : function(file,data,response) {
        //console.log(data);
        var filedata = JSON.parse(data);
        that.model.qrcode_path = filedata.file_path;
        $("#file_uploaded2").val(filedata.file_path);
        $("#file_uploaded21").text(filedata.file_path+"上传完成");
      }
    });

  }
}
